package com.eatme.web.app.coupon.template.entity;

import com.eatme.web.app.coupon.common.constant.CouponCategory;
import com.eatme.web.app.coupon.template.converter.CouponCategoryConverter;
import com.eatme.web.app.coupon.template.converter.DistributeTargetConverter;
import com.eatme.web.app.coupon.template.converter.ProductLineConverter;
import com.eatme.web.app.coupon.template.converter.TemplateRuleConverter;
import com.eatme.web.app.coupon.template.serialization.CouponTemplateSerialize;
import com.eatme.web.app.coupon.common.constant.DistributeTarget;
import com.eatme.web.app.coupon.common.constant.ProductLine;
import com.eatme.web.app.coupon.common.vo.TemplateRule;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 优惠券模板实体类定义: 基础属性 + 规则属性
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name="coupon_template")
@JsonSerialize(using = CouponTemplateSerialize.class)
public class CouponTemplate implements Serializable {

    /** 自增主键 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",nullable = false)
    private Integer id;

    /** 是否可用状态 */
    @Column(name = "available",nullable = false)
    private Boolean available;

    /** 是否过期 */
    @Column(name = "expired",nullable = false)
    private  Boolean expired;

    /** 优惠券名称 */
    @Column(name = "name",nullable = false)
    private String name;

    /** 优惠券 logo */
    @Column(name = "logo",nullable = false)
    private String logo;

    /** 优惠券描述 */
    @Column(name = "intro",nullable = false)

    private String desc;

    /** 优惠券分类 */
    @Column(name = "category",nullable = false)
    @Convert(converter = CouponCategoryConverter.class)
    private CouponCategory category;

    /** 产品线 */
    @Column(name = "product_line",nullable = false)
    @Convert(converter = ProductLineConverter.class)
    private ProductLine productLine;

    /** 总数 */
    @Column(name = "coupon_count",nullable = false)
    private  Integer count;

    /** 创建时间 */
    @CreatedDate
    // @UpdateTimestamp
    // @CreatedBy
    @Column(name = "create_time",nullable = false)
    private Date createTime;

    /** 创建用户 */
    @Column(name = "user_id",nullable = false)
    private Long userId;

    /** 优惠券模板编码 */
    @Column(name = "template_key",nullable = false)
    private  String key;

    /** 目标用户 */
    @Column(name = "target",nullable = false)
    @Convert(converter = DistributeTargetConverter.class)
    private DistributeTarget target;

    /** 优惠券规则 */
    @Column(name = "rule",nullable = false)
    @Convert(converter = TemplateRuleConverter.class)
    private TemplateRule rule;

    public CouponTemplate() {
    }

    public CouponTemplate(Integer id, Boolean available, Boolean expired, String name, String logo, String desc, CouponCategory category, ProductLine productLine, Integer count, Date createTime, Long userId, String key, DistributeTarget target, TemplateRule rule) {
        this.id = id;
        this.available = available;
        this.expired = expired;
        this.name = name;
        this.logo = logo;
        this.desc = desc;
        this.category = category;
        this.productLine = productLine;
        this.count = count;
        this.createTime = createTime;
        this.userId = userId;
        this.key = key;
        this.target = target;
        this.rule = rule;
    }

    /** 自定义构造函数 */
    public CouponTemplate(String name, String logo, String desc, String category, Integer productLine, Integer count, Long userId, Integer target, TemplateRule rule) {
        this.available =false;
        this.expired =false;
        this.name = name;
        this.logo = logo;
        this.desc = desc;
        this.category = CouponCategory.of(category);
        this.productLine = ProductLine.of(productLine);
        this.count = count;
        this.userId = userId;
        // 优惠券模板唯一编码 = 4(产品线和类型) + (日期: 20230101) + id(扩充为4位)
        this.key = productLine.toString() +category + new SimpleDateFormat("yyyymmdd").format(new Date());
        this.target = DistributeTarget.of(target);
        this.rule = rule;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Boolean getAvailable() {
        return available;
    }

    public void setAvailable(Boolean available) {
        this.available = available;
    }

    public Boolean getExpired() {
        return expired;
    }

    public void setExpired(Boolean expired) {
        this.expired = expired;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public CouponCategory getCategory() {
        return category;
    }

    public void setCategory(CouponCategory category) {
        this.category = category;
    }

    public ProductLine getProductLine() {
        return productLine;
    }

    public void setProductLine(ProductLine productLine) {
        this.productLine = productLine;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public DistributeTarget getTarget() {
        return target;
    }

    public void setTarget(DistributeTarget target) {
        this.target = target;
    }

    public TemplateRule getRule() {
        return rule;
    }

    public void setRule(TemplateRule rule) {
        this.rule = rule;
    }
}
